﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Portal.Code
{
    public class PaginationHelper
    {
        public static Pagination GetPaginationInfo(int totalPageNum, int curPageNum)
        {
            int start, end;
            if (totalPageNum <= Constant.PaginationItemCount)
            {
                start = 1;
                end = totalPageNum;
            }
            else
            {
                int half = int.Parse(decimal.Floor((decimal) Constant.PaginationItemCount/2).ToString());
                if (curPageNum > half)
                {
                    end = curPageNum + half > totalPageNum ? totalPageNum : curPageNum + half;
                    start = end - Constant.PaginationItemCount + 1;
                }
                else
                {
                    start = curPageNum - half < 1 ? 1 : curPageNum - half;
                    end = start + Constant.PaginationItemCount - 1;
                }
            }

            return new Pagination
                       {
                           HasItems = totalPageNum > 0,
                           StartPageNum = start,
                           EndPageNum = end,
                           CurPageNum = curPageNum,
                           PrePageNum = curPageNum - 1 < 1 ? 1 : curPageNum - 1,
                           NextPageNum = curPageNum + 1 > totalPageNum ? totalPageNum : curPageNum + 1,
                       };
        }
    }

    public class Pagination
    {
        public int StartPageNum { get; set; }
        public int EndPageNum { get; set; }
        public int CurPageNum { get; set; }
        public int PrePageNum { get; set; }
        public int NextPageNum { get; set; }
        public bool HasItems { get; set; }
    }
}